package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityScreenBrightnessBinding;
import com.apex.aolonfitdemo.dialog.NotDisturbDialog;
import com.apex.aolonfitdemo.dialog.SwitchDialog;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.callback.GeneralCallback;
import com.apex.ax_bluetooth.callback.RaiseHandBrightScreenCallback;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.apex.ax_bluetooth.enumeration.QueryWatchInfoType;
import com.apex.ax_bluetooth.model.EABleGesturesBrightScreen;
import com.cyber.bridge.module.bean.RaiseLightScreenEntity;
import com.cyber.bridge.module.listener.OnCyberQueryAndSetStateListener;


public class BrightScreenActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    private SwitchDialog ageDialog;
    private NotDisturbDialog startDialog;
    private NotDisturbDialog endDialog;
    private int startHour;
    private int startMinute;
    private int endHour;
    private int endMinute;
    private String tempRest;

    ActivityScreenBrightnessBinding activityScreenBrightnessBinding;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                RaiseLightScreenEntity raiseLightScreenEntity = (RaiseLightScreenEntity) msg.obj;
                if (raiseLightScreenEntity == null) {
                    return;
                }
                startMinute = raiseLightScreenEntity.getStartTime() % 60;
                startHour = raiseLightScreenEntity.getStartTime() / 60;
                endMinute = raiseLightScreenEntity.getEndTime() % 60;
                endHour = raiseLightScreenEntity.getEndTime() / 60;
                tempRest = raiseLightScreenEntity.isEnabled() ? getString(R.string.switch_state_on) : getString(R.string.switch_state_close);
                activityScreenBrightnessBinding.screenBrightness.setText(tempRest);

                activityScreenBrightnessBinding.startTime.setText((startHour < 10 ? "0" + startHour : startHour + "") + ":" + (startMinute < 10 ? "0" + startMinute : startMinute + ""));
                activityScreenBrightnessBinding.endTime.setText((endHour < 10 ? "0" + endHour : endHour + "") + ":" + (endMinute < 10 ? "0" + endMinute : endMinute + ""));


            } else if (msg.what == 0x42) {
                Log.e(TAG, "成功");
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {
                Log.e(TAG, "失败");
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(BrightScreenActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityScreenBrightnessBinding = ActivityScreenBrightnessBinding.inflate(getLayoutInflater());
        setContentView(activityScreenBrightnessBinding.getRoot());
        activityScreenBrightnessBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityScreenBrightnessBinding.tool);
        getSupportActionBar().setTitle(null);
        activityScreenBrightnessBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnRaiseLightScreenListener(new OnCyberQueryAndSetStateListener<RaiseLightScreenEntity>() {
            @Override
            public void queryConfig(RaiseLightScreenEntity raiseLightScreenEntity) {
                if (mHandler != null) {
                    Message message = mHandler.obtainMessage();
                    message.what = 0x40;
                    message.obj = raiseLightScreenEntity;
                    mHandler.sendMessage(message);
                }
            }

            @Override
            public void setConfig(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }
            }
        });
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(BrightScreenActivity.this);
        }
        waitingDialog.show();
        AolonFitManager.getInstance().queryRaiseLightScreen();
        activityScreenBrightnessBinding.screenBrightness.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ageDialog == null) {
                    ageDialog = new SwitchDialog(BrightScreenActivity.this);
                    ageDialog.setSelectListener(new SwitchDialog.SelectListener() {
                        @Override
                        public void selectData(String sex) {
                            activityScreenBrightnessBinding.screenBrightness.setText(sex);
                            tempRest = sex;

                        }
                    });
                }
                if (!ageDialog.isShowing()) {
                    ageDialog.show();
                }
            }
        });
        activityScreenBrightnessBinding.startTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (startDialog == null) {
                    startDialog = new NotDisturbDialog(BrightScreenActivity.this, getString(R.string.start_time));
                    startDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {
                            startHour = hour;
                            startMinute = minute;
                            activityScreenBrightnessBinding.startTime.setText((startHour < 10 ? "0" + startHour : startHour + "") + ":" + (startMinute < 10 ? "0" + startMinute : startMinute + ""));
                        }
                    });
                }
                startDialog.show();
            }
        });
        activityScreenBrightnessBinding.endTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (endDialog == null) {
                    endDialog = new NotDisturbDialog(BrightScreenActivity.this, getString(R.string.end_time));
                    endDialog.setSelectListener(new NotDisturbDialog.SelectListener() {
                        @Override
                        public void selectData(int hour, int minute) {
                            endHour = hour;
                            endMinute = minute;
                        }
                    });
                }
                endDialog.show();
            }
        });
        activityScreenBrightnessBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                waitingDialog.show();
                AolonFitManager.getInstance().setRaiseLightScreen(new RaiseLightScreenEntity(getString(R.string.switch_state_on).equalsIgnoreCase(tempRest) ? true : false, startHour * 60 + startMinute, endHour * 60 + endMinute));
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        if (ageDialog != null) {
            ageDialog.dismiss();
            ageDialog.destroyDialog();
            ageDialog = null;
        }
        if (startDialog != null) {
            startDialog.destroyDialog();
            startDialog = null;
        }
        if (endDialog != null) {
            endDialog.destroyDialog();
            endDialog = null;
        }
        super.onDestroy();
    }

}
